//index.js
//获取应用实例
var app = getApp();

Page({
    data: {
        scrollTop: {
            scroll_top: 0,
        },
 
        showPulldown: false,
        isFixed: false,
        isshow_subsidy: true,
    
        redTriangle: false,
       
        sort: 0,
        cname: "",
        sortName: "默认",
        currentTab: 0,
        currentTab2: 0,
        hiddenLoading: false,
        indicatorDots: true,
        autoplay: true,
        interval: 3000,
        from_uid: 0,
        nickname: '',
        memberid: 0,
        datalock: false,
   
     
        showAnimation: false,
        globalset: {

        },
   
        duration: 100,
    
        commodity: [

        ],
        goodCarousel: [

        ],
        goodCarousel2: [

        ],
    
        activitymenu: [

        ],
    },

    onLoad: function (options) {
     
        //app.setTabBar();
        var $this = this;
        if (options.id) {
          var   cateid = options.id
          $this.setData({ 
              cateid: cateid,
              jinxuanid: cateid,
              cname_show: cateid
              })
        }else{
            $this.setData({ 
                cateid: 0,
                jinxuanid:0,
                cname_show: 0
                 })
            
        }
        //扫二维码进来的推荐人ID获取
        if (typeof options != "undefined" && typeof options.scene != "undefined") {
            wx.setStorage({
                key: "from_uid",
                data: options.scene
            });
            this.setData({
                from_uid: options.scene,
                datalock: false
            });

        }
        this.getGlobalSet();
      
    },
    initIndex: function () {
        var $this = this;
      
        $this.goodList(1);
       
        var memberinfo = wx.getStorageSync('memberinfo');
        $this.setData({ memberinfo: memberinfo });
        var globalset = wx.getStorageSync('globalset');
        $this.setData({
            commission: 0,
            datalock: false
        });
        //计算总的佣金比例
        var total_commission = 0;
        if (globalset.memberlevel != undefined && globalset.memberlevel.length != undefined) {
            for (var i = 0; i < globalset.memberlevel.length; i++) {
            
                //当前会员类型的等级佣金比例
                if (globalset.memberlevel[i].type == memberinfo.type && globalset.memberlevel[i].name == memberinfo.level) {
                    total_commission = parseFloat(globalset.memberlevel[i].myteam_credit2) + parseFloat(globalset.memberlevel[i].myleader1_credit2) + parseFloat(globalset.memberlevel[i].myleader2_credit2);
                    //换成只取1级佣金的比例
                    total_commission = parseFloat(globalset.memberlevel[i].myteam_credit2);
                  
                    $this.setData({
                        commission: total_commission,
                        datalock: false
                    });
                   
                }
            }
        }
        if (memberinfo.type == 0) {
            if (globalset.isopen_subsidy == 0) {
                $this.setData({
                    isshow_subsidy: false
                });
            }
         
            total_commission = parseFloat(globalset.subsidy);
            $this.setData({
                commission: total_commission,
                datalock: false
            });
        }

        if (globalset.isshow_subsidy == 0) {
            $this.setData({
                isshow_subsidy: false
            });
        }
    },
    getGlobalSet: function () {
        var $this = this;
        app.util.request({
            url: 'entry/wxapp/Global',
            showLoading: false,
            data: {
                "m": 'nets_haojk'
            },
            method: 'post',
            success: function (response) {
                var globalset = response.data.data;
                $this.setData({
                    hiddenLoading: true
                });
                $this.setData({
                    globalset: globalset,
                    homeskinid: globalset.homeskinid
                });
                wx.setStorage({
                    key: "globalset",
                    data: globalset
                });

                $this.setData({
                    hiddenLoading: true
                });
                $this.getMember();

            }
        });

    },
    getMember: function () {
        var that = this;
        app.getUserInfo(function (userInfo) {
            //更新数据
            app.util.getUserInfo(function (response) {

                response.memberInfo.nickname = userInfo.nickName;
                response.memberInfo.avatar = userInfo.avatarUrl;
                response.memberInfo.city = userInfo.city;
                response.memberInfo.country = userInfo.country;
                response.memberInfo.province = userInfo.province;
                response.memberInfo.gender = userInfo.gender;
                that.addMember(response.memberInfo);
            });
        });
        // 页面渲染完成
    },
  
    addMember: function (userInfo) {
        var $this = this;
        app.util.request({
            url: 'entry/wxapp/AddMember',
            showLoading: false,
            data: {
                "m": 'nets_haojk',
                "from_uid": this.data.from_uid,
                "nickname": userInfo.nickname,
                "avatar": userInfo.avatar,
                "city": userInfo.city,
                "country": userInfo.country,
                "province": userInfo.province,
                "gender": userInfo.gender,
            },
            method: 'post',
            success: function (response) {
                //首页存储会员对象到缓存，其他页面直接读取缓存即可
                wx.setStorage({
                    key: "memberinfo",
                    data: response.data.data
                });
                $this.setData({
                    memberid: response.data.data.memberid,
                    nickname: response.data.data.nickname,
                    datalock: false
                });
                //取会员对象的方式
                wx.getStorage({
                    key: 'memberinfo',
                    success: function (res) {
                    }
                });
           
                //获取用户自定义
                $this.getUseCate();
            }
        });
    },

    ////不跳转到精选页面
    bycname1: function (e) {
        var $this = this;

        wx.getSystemInfo({
            success: function (res) {
                $this.setData({
                    scrollHeight: res.windowHeight
                });
            }
        });

     
        var cname = "";
        if (e.currentTarget.dataset.url) {

            cname = e.currentTarget.dataset.url.split("=")[1];
        }

        this.setData({
            cname_show: e.currentTarget.dataset.cateid,
            cname: cname,

            cateid: e.currentTarget.dataset.cateid,
            keyword: "",
        });
        console.log($this.data.cname_show);

        this.setData({
            commodity: [],
            datalock: false
        });

        wx.setStorage(
            {key: "cname",data: e.currentTarget.dataset.cname},
            {key: "searchkey", data: "" }
            
        );
        this.goodList(1);
    },
   
    limittimeMore: function (e) {
        wx.setStorageSync("searchkey", "");
        wx.setStorageSync("limittime", 1);
        wx.setStorageSync("realtime", "");
        wx.navigateTo({
            url: "../search/index",
        })
    },

    onShareAppMessage: function (res) {
        var memberinfo = wx.getStorageSync("memberinfo");
        if (res.from === 'button') {
            // 来自页面内转发按钮
        }

        return {
            title: this.data.globalset.title,
            path: '/haojk/pages/index/index?from_uid=' + memberinfo.memberid + '&from_nickname=' + memberinfo.nickname + '&type=' + memberinfo.type + '&homeskinid=' + memberinfo.homeskinid,
            success: function (res) {
                // 转发成功
            },
            fail: function (res) {
                // 转发失败
            }
        }

    },


    goodList: function (page, t, callback, goodstype) {
        var $this = this;
        var limittime = 1;
        var realtime = 1;
        if (t == "limittime") {
            realtime = "";
        } else if (t == "realtime") {
            limittime = "";
        } else {
            //  $this.setData({canBottomRefresh:true});
            limittime = "";
            realtime = "";
        }
        if ($this.data.datalock) {
            //return false;
        }
        $this.setData({
            page: page,
            datalock: true,
        });

        app.util.request({
            url: 'entry/wxapp/GetUseCateGoods',
            showLoading: false,
            data: {
                m: 'nets_haojk',
                page: page,
              
                cateid: $this.data.cateid
            },
            method: 'post',
            success: function (response) {
                if (typeof callback == 'function') {
                    callback(response.data.data);
                    $this.setData({
                        datalock: false
                    });
                } else {
                
                    var goods = $this.data.commodity;
                    if (page == 1) {
                        goods = new Array();
                    }
                    var shishigoods = Array();
                    var jinrigoods = Array();
                    if (t == "limittime") {

                        goods = $this.data.goodCarousel;
                    } else if (t == "realtime") {
                        goods = $this.data.goodCarousel2;
                    }
                    if (response.data.data) {
                        var row = 0;
                        for (var i in response.data.data) {
                            row++;
                            if ($this.data.commission == 0) {
                                response.data.data[i].wlPrice_after1 = response.data.data[i].discount;
                            } else {
                                response.data.data[i].wlPrice_after1 = response.data.data[i].wlPrice_after * (response.data.data[i].wlCommissionShare / 100) * ($this.data.commission / 100) * 0.9;
                                if (response.data.data[i].wlPrice_after1 > 0)
                                    response.data.data[i].wlPrice_after1 = response.data.data[i].wlPrice_after1.toFixed(2);
                              

                            }
                            if (row <= 6 && page == 1) {
                                shishigoods.push(response.data.data[i]);
                            } else if (row > 6 && row <= 12 && page == 1) {
                                jinrigoods.push(response.data.data[i]);
                            } else {
                                goods.push(response.data.data[i]);
                            }
                        }
                    }
                    if (t == "limittime") {
                        $this.setData({
                            goodCarousel: goods,
                            datalock: false
                        });
                    } else if (t == "realtime") {
                        $this.setData({
                            goodCarousel2: goods,
                            datalock: false
                        });
                    
                    } else {
                        if (page == 1) {
                            $this.setData({
                                goodCarousel: shishigoods,
                                datalock: false
                            });

                            $this.setData({
                                goodCarousel2: jinrigoods,
                                datalock: false
                            });
                        }
                        $this.setData({
                            commodity: goods,
                            datalock: false
                        });
                    }
                }
            }
        });
    },

    //事件处理函数
    skiptodetail: function (e) {

        var skuId = e.currentTarget.dataset.skuid;
        for (var i = 0; i < this.data.commodity.length; i++) {
            if (this.data.commodity[i].skuId == skuId) {
                var good = this.data.commodity[i];
                wx.setStorage({
                    key: "good",
                    data: good
                });
            }
        }

        for (var i = 0; i < this.data.goodCarousel.length; i++) {
            if (this.data.goodCarousel[i].skuId == skuId) {
                var good = this.data.goodCarousel[i];
                wx.setStorage({
                    key: "good",
                    data: good
                });
            }
        }
        for (var i = 0; i < this.data.goodCarousel2.length; i++) {
            if (this.data.goodCarousel2[i].skuId == skuId) {
                var good = this.data.goodCarousel2[i];
                wx.setStorage({
                    key: "good",
                    data: good
                });
            }
        }

        wx.navigateTo({
            url: '../detail/index?skuId=' + skuId
        })
    },
    onReachBottom: function () {

        var $this = this;

        var page = $this.data.page + 1;
        var second_height = $this.data.second_height * page;
        $this.setData({ second_height: second_height });
        $this.goodList(page);

    },
   
    changePullDState: function () {
      
        var $that = this;
        if ($that.data.showPulldown) {
            $that.setData({
                showPulldown: false,
            })
        } else {
            $that.setData({
                showPulldown: true,
            })
        }
    },
    getUseCate:function(){
        var $this =this;
        app.util.request({
            url: 'entry/wxapp/GetUseCate',
            showLoading: false,
            data: {
                "m": 'nets_haojk'
            },
            method: 'post',
            success: function (response) {
                if(response.data.errno==0){
                    var setArray = response.data.data;
                    $this.setData({ personalSetArray: setArray});
                    $this.initIndex();
                }        
            }
        })
    }  
})
